<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\Common\Constants\PayWayConstants;
use App\CustHub\Constants\OrderStatusConstants;
use App\CustHub\Constants\OrderTypeConstants;
use App\CustHub\Mapper\ChOrderMapper;
use Mine\Abstracts\AbstractService;

class ChOrderService extends AbstractService
{
    public $mapper;

    public function __construct(ChOrderMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    public function rechargeOrderList(array $params)
    {
        $data = $this->mapper->model::query()
            ->select(['id', 'order_no', 'pay_amount', 'pay_way', 'remark', 'status', 'pay_time', 'order_type', 'created_at'])
            ->where('uid', $params['main_uid'])
            ->orderBy('created_at', 'desc')
            ->paginate((int)$params['pageSize'] ?? 10);

        $data->getCollection()->transform(function ($item) {
            $item->order_type_text = OrderTypeConstants::getMessage($item['order_type']);
            $item->status_text = OrderStatusConstants::getMessage($item['status']);
            $item->pay_way_text = PayWayConstants::getMessage($item['pay_way']);
            return $item;
        });

        $result = $this->mapper->setPaginate($data, $params);
        return $result;
    }
}